﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;


namespace GameServer.Managers
{

    class MapVehicleSeats
    {

        private static Dictionary<string, VehicleSeat[]> _Seats;
        public static void Load()
        {
            try
            {
                MapVehicleSeats._Seats = new Dictionary<string, VehicleSeat[]>();
                List<VehicleSeat> tmpSeats = new List<VehicleSeat>();

                int[] tableIDs = DB.runReadColumn("SELECT id FROM vehicle_seats;", 0, null);

                for (int I = 0; I < tableIDs.Length; I++)
                {
                    string[] Data = DB.runReadRow("SELECT * FROM `vehicle_seats` WHERE id=" + tableIDs[I]);
                    string vehicleCode = Data[1];
                    string seatsString = Data[2];
                    string[] seatsSplit = seatsString.Split(new char[] { ';' });
                    foreach (string seat in seatsSplit)
                    {
                        string[] seatDataSplit = seat.Split(new char[] { ':' });
                        tmpSeats.Add(new VehicleSeat(int.Parse(seatDataSplit[0]), int.Parse(seatDataSplit[1]), seatDataSplit[2]));
                    }
                    MapVehicleSeats._Seats.Add(vehicleCode, tmpSeats.ToArray());
                }

                Log.WriteLine("Succesful loaded [ " + MapVehicleSeats._Seats.Count + " ] VehicleSeats");
            }
            catch
            {
            }
        }

        public static VehicleSeat[] GetSeats(string code)
        {
            try{
            if (MapVehicleSeats._Seats.ContainsKey(code))
            {
                return MapVehicleSeats._Seats[code];
            }
            else
            {
                //No seats in these "vehicles"
                if (code.Substring(0, 2) != "EN")
                {
                    Log.WriteError("Vehicle Seat [" + code + "] not found or not loaded");
                    return null;
                }
                return new VehicleSeat[0];
            }
            }
            catch { return null; }
        }
    }

    class MapVehicles
    {

        private static List<Vehicle>[] _Vehicles;
        public static void Load()
        {
            try
            {
                MapVehicles._Vehicles = new List<Vehicle>[56];
                for (int i = 0; i <= 55; i++)
                {
                    MapVehicles._Vehicles[i] = new List<Vehicle>();
                }

                int[] tableIDs = DB.runReadColumn("SELECT id FROM vehicle;", 0, null);

                for (int I = 0; I < tableIDs.Length; I++)
                {
                    string[] Data = DB.runReadRow("SELECT * FROM `vehicle` WHERE id=" + tableIDs[I]);
                    int mapID = int.Parse(Data[1]);
                    string myCode = Data[2];
                    MapVehicles._Vehicles[mapID].Add(new Vehicle(myCode, int.Parse(Data[4]), MapVehicleSeats.GetSeats(myCode), int.Parse(Data[12]), new int[] {
				        int.Parse(Data[6]),
				       int.Parse(Data[7]),
				        int.Parse(Data[8])
			        }, new int[] {
				     int.Parse(Data[9]),
				       int.Parse(Data[10]),
				        int.Parse(Data[11])
			        }));

                }

                Log.WriteLine("Succesful loaded [ " + MapVehicles._Vehicles.Length + " ] Vehicles");
            }
            catch
            {
            }
        }

        public static Vehicle[] GetVehicles(int Map)
        {
            return MapVehicles._Vehicles[Map].ToArray();
        }
    }
}
